/**
 * [ Load Pading 1.0]
 * @Author   :seven
 * @DateTime :2016-05-09T12:05:52+0800
 * @Detail   :用于缓存式分页,加载数据。
 * Example :
 *        var pageObj = new LoadPading(5,3,"/AutoReturn/Admin/Index/demoAjax");
 *    pageObj.initFun_Load();
 */

//是否加载页码
var next_number_sign = false;
//加入的A标签样式
var number_span_html = '';
var search_condition = '';
/*以下全局变量不可修改,由运行状态自行修改*/
//所在页码
var current_page = 0;
//页面总数
var pageSum;
//剩于总数
var pageLes = 0;
//预约加载所在页
var pagePos = 0;
//预加载数据集
var dataBox;

function LoadPading(size, Load, url, count, search_true) {
    /*全局通用静态变量*/
    this.Load_pageSize = size,	//单页面容量
        this.Load_pageLoad = Load,	//预加载数量
        this.Load_dateUrl = url;  //加载数据后台地址

    this.number_span_count = count;//显示的页码数量

    this.search_sign = search_true;//是否包括查询
}
LoadPading.prototype.TEST_IMPORT = function () {
    alert("Welcome To Use The Load Page");
}
//搜索
LoadPading.prototype.init_search_View = function () {
    current_page = 1;
    pageLes = this.Load_pageLoad - 1;
    /*赋值获取预加载的数量*/
    pagePos = 0;
    this.Ajax_Data_Get(1, true);
    this.init_number_View();
}
/*初始化数据窗口*/
LoadPading.prototype.initFun_Load = function (sign_bind) {
    //初始化所有变量.
    if (this.Load_pageLoad < 3) {
        this.Load_pageLoad = 3;
    }
    if (sign_bind != false) {
        $("#nextPage").on("click", function () {
            LoadObj.nextPage()
        });
        $("#prevPage").on("click", function () {
            LoadObj.prevPage()
        });
    }

    if (!this.search_sign) {
        this.init_search_View();
    }
}

LoadPading.prototype.init_number_View = function () {
    number_span_html = '';
    next_number_sign = false;
    var html = '<a href="javascript:click_number_css(1)" class="number current" title="1">1</a> ';

    for (var i = 2; i <= this.number_span_count; i++) {
        html += '<a href="javascript:click_number_css(' + i + ')" class="number" title="' + i + '">' + i + '</a> ';
    }
    $("#number_span").html(html);
}
/*获取数据包*/
LoadPading.prototype.Ajax_Data_Get = function (data_page, sign) {
    $.post(this.Load_dateUrl,
        {
            "page": data_page,  //赋值
            "pageSize": this.Load_pageSize, //全局获取
            "pageLoad": this.Load_pageLoad,
            "condition": search_condition,	//条件
        }, function (data) {
            pageSum = data["pageSum"];
            dataBox = eval(data["dataBox"]);
            console.log(data);
            if (pageSum == 0) {
                alert("无有效数据");
                $("#number_span").html("");
            }
            if (sign) {
                /*初始化传入时需要重新加载页面*/
                LoadPading.prototype.setDateScene(dataBox[pagePos]);
            }
        }, "json");
}
/*重写的数据窗口部署*/
LoadPading.prototype.setDateScene = function (useSet) {
    //body
    alert("重写的数据窗口部署\nLoadPading.prototype.setDateScene = function (useSet){//body}\n使用参数:\nuseSet=-=数据集\ncurrent_page=-=当前页码\npageSum=-=总页码");
};

/*下一页事件*/
LoadPading.prototype.nextPage = function () {
    if (current_page % this.number_span_count != 0) {
        next_number_sign = false;
        html = '';
    }
    if (current_page < pageSum) {
        current_page++;
        pageLes--;  //剩于减少
        pagePos++;  //向后位移
    } else {
        return;
    }
    this.next_page_Css();
    /*若是由点击上一页到达则显示为pagePos才到达pagePos,此时点击则需要重新加载数据*/
    if (pagePos == this.Load_pageLoad) {
        pagePos = 0;
        pageLes = this.Load_pageLoad - 1;
        this.Ajax_Data_Get(current_page + 1, true);
        return;
    }

    //布置数据到场景
    this.setDateScene(dataBox[pagePos]);
    /*当游标到达数据块的最一块时*/
    if (pageLes <= 0) {
        /**
         预加载了新的数据块,此时游标处于上一层数据块中
         剩于的数据块为预加载数量.游标位于-1处
         */
        pageLes = this.Load_pageLoad;
        pagePos = -1;
        //调用AJAX加载数据
        this.Ajax_Data_Get(current_page + 1, false);
    }
};

/*上一页事件*/
LoadPading.prototype.prevPage = function () {
    if (current_page > 1) {
        current_page--;
        pageLes++;  //剩于增加
        pagePos--;  //向前位移
    } else {
        return;
    }
    this.prev_page_Css();
    if (pagePos <= -1) {
        if (pagePos == -1) {
            pageLes = 0;
            /*赋值剩于数的数量*/
            pagePos = this.Load_pageLoad - 1;
            /*赋值现在游标的数量*/
            this.Ajax_Data_Get(current_page - 1, true);
        } else {
            this.Ajax_Data_Get(current_page - 1, true);
            pagePos = this.Load_pageLoad - 2;
            pageLes = 1;
        }
    } else {
        this.setDateScene(dataBox[pagePos]);
    }
}

/**
 * [next_page_Css 点击下一页的样式变化]
 * @Author   :seven
 * @DateTime :2016-06-03T00:33:44+0800
 * @Detail   :
 * @return   {[type]}    [description]
 */
LoadPading.prototype.next_page_Css = function () {
    var next_object = $("#pageLoad").find(".current").next();

    $("#pageLoad").find(".current").attr("class", "number");
    next_object.attr("class", "number current");

    if (next_number_sign) {
        next_number_sign = false;
        $("#number_span").html(number_span_html);
        html = '';
    }
    if (current_page % this.number_span_count == 0) {
        var index_page_number = current_page + 1;
        var index_page_end = index_page_number + this.number_span_count;
        number_span_html = '<a href="javascript:click_number_css(' + index_page_number + ')" class="number current" title="' + index_page_number + '">' + index_page_number + '</a> ';

        if (index_page_number + this.number_span_count > pageSum) {
            index_page_end = pageSum + 1;
        }
        for (var i = index_page_number + 1; i < index_page_end; i++) {
            number_span_html += '<a href="javascript:click_number_css(' + i + ')" class="number" title="' + i + '">' + i + '</a> ';
        }
        next_number_sign = true;
    }
}

/**
 * [jump_number_Css 点击页码跳转的样式变化]
 * @Author   :seven
 * @DateTime :2016-06-03T00:34:20+0800
 * @Detail   :
 * @param    {[type]}    number [点击的页码]
 * @return   {[type]}    [description]
 */
LoadPading.prototype.jump_number_Css = function (number) {
    if ($("#pageLoad").find(".current").text() == number) {
        return;
    }
    $("#pageLoad").find(".current").attr("class", "number");
    $("#pageLoad").find("[title=" + number + "]").attr("class", "number current");
}
/**
 * [jump_number_page 点击页码跳转数据处理]
 * @Author   :seven
 * @DateTime :2016-06-03T00:35:16+0800
 * @Detail   :
 * @param    {[type]}    number [description]
 * @return   {[type]}    [description]
 */
LoadPading.prototype.jump_number_page = function (number) {
    LoadPading.prototype.jump_number_Css(number);
    var old_page = current_page;
    var old_les = pageLes;
    var old_pos = pagePos;
    // console.log('this:', this)
    // console.log('number:', number)
    // console.log('Load_pageLoad:',this.Load_pageLoad)
    // console.log('pageLes:',pageLes)
    // console.log('pagePos:',pagePos)
    // console.log('number_span_count:',this.number_span_count)
    // console.log('pageSum:',pageSum)

    current_page = number;
    var current_pos = number % this.Load_pageLoad;

    pageLes = this.Load_pageLoad - current_pos;
    if (pageLes == this.Load_pageLoad) {
        pageLes = 0;
    }
    pagePos = this.Load_pageLoad - 1 - pageLes;

    if (current_page % this.number_span_count == 0) {
        console.log('1', current_page % this.number_span_count)
        var index_page_number = current_page + 1;
        var index_page_end = index_page_number + this.number_span_count;
        number_span_html = '<a href="javascript:click_number_css(' + index_page_number + ')" class="number current" title="' + index_page_number + '">' + index_page_number + '</a> ';

        if (index_page_number + this.number_span_count > pageSum) {
            index_page_end = pageSum + 1;
        }
        for (var i = index_page_number + 1; i < index_page_end; i++) {
            number_span_html += '<a href="javascript:click_number_css(' + i + ')" class="number" title="' + i + '">' + i + '</a> ';
        }
        next_number_sign = true;
    }
    /*处理数据块*/
    if (current_page - old_page > 0) {
        /*往后跳页*/
        if (current_page - old_page <= old_les) {
            if (pageLes == 0) {
                this.setDateScene(dataBox[pagePos]);
                pageLes = this.Load_pageLoad;
                pagePos = -1;
                this.Ajax_Data_Get(current_page + 1, false);
            } else {
                /*缓存内,非临界点,直接使用缓存数据*/
                this.setDateScene(dataBox[pagePos]);
            }
        } else {
            if (pageLes == 0) {
                this.Ajax_Data_Get(current_page - 1, true);
            } else {
                this.Ajax_Data_Get(current_page, true);
            }
        }
    } else {
        /*往前跳页*/
        if (old_page - current_page <= old_pos) {
            /*缓存内跳转,直接使用页面缓存数据*/
            if (old_pos == -1) {
                this.Ajax_Data_Get(current_page - 1, true);
            } else {
                this.setDateScene(dataBox[pagePos]);
            }
        } else {
            /*缓存外跳转,重新拉取新的数据源*/
            this.Ajax_Data_Get(current_page - 1, true);
        }
    }
}

/**
 * [jump_bottom 最后一页]
 * @Author   :seven
 * @DateTime :2016-06-03T00:35:46+0800
 * @Detail   :
 * @return   {[type]}                  [description]
 */
LoadPading.prototype.jump_bottom = function () {
    if ($("#pageLoad").find(".current").text() == pageSum) {
        return;
    }
    current_page = pageSum;

    var current_pos = current_page % this.Load_pageLoad;

    pageLes = this.Load_pageLoad - current_pos;
    if (pageLes == this.Load_pageLoad) {
        pageLes = 0;
    }
    pagePos = this.Load_pageLoad - 1 - pageLes;

    var current_number = current_page % this.number_span_count;
    var current_number_pos = parseInt(current_page / this.number_span_count);
    var html = '';
    for (var i = 1; i < current_number; i++) {
        var tmp_index = current_number_pos * this.number_span_count + i;
        html += '<a href="javascript:click_number_css(' + tmp_index + ')" class="number" title="' + tmp_index + '">' + tmp_index + '</a> ';
    }
    html += '<a href="javascript:click_number_css(' + (current_number_pos * this.number_span_count + current_number) + ')" class="number current" title="' + (current_number_pos * this.number_span_count + current_number) + '">' + (current_number_pos * this.number_span_count + current_number) + '</a> '

    $("#number_span").html(html);
    this.Ajax_Data_Get(current_page - 1, true);
}
/**
 * [jump_top 第一页]
 * @Author   :seven
 * @DateTime :2016-06-03T00:35:56+0800
 * @Detail   :
 * @return   {[type]}                  [description]
 */
LoadPading.prototype.jump_top = function () {
    if ($("#pageLoad").find(".current").text() == 1) {
        return;
    }
    this.initFun_Load(false);
}

/**
 * [prev_page_Css 上一页的样式变化]
 * @Author   :seven
 * @DateTime :2016-06-03T00:36:05+0800
 * @Detail   :
 * @return   {[type]}    [description]
 */
LoadPading.prototype.prev_page_Css = function () {
    var prev_object = $("#pageLoad").find(".current").prev();

    $("#pageLoad").find(".current").attr("class", "number");
    prev_object.attr("class", "number current");

    number_span_html = '';
    next_number_sign = false;
    if (current_page % this.number_span_count == 0) {
        var index_page_end = current_page;
        var index_page_number = index_page_end - this.number_span_count + 1;

        for (var i = index_page_number; i < index_page_end; i++) {
            number_span_html += '<a href="javascript:click_number_css(' + i + ')" class="number" title="' + i + '">' + i + '</a> ';
        }
        number_span_html += '<a href="javascript:click_number_css(' + index_page_end + ')" class="number current" title="' + index_page_end + '">' + index_page_end + '</a> ';
        $("#number_span").html(number_span_html);

        index_page_number = current_page + 1;
        index_page_end = index_page_number + this.number_span_count;
        number_span_html = '<a href="javascript:click_number_css(' + index_page_number + ')" class="number current" title="' + index_page_number + '">' + index_page_number + '</a> ';

        if (index_page_number + this.number_span_count > pageSum) {
            index_page_end = pageSum + 1;
        }
        for (var i = index_page_number + 1; i < index_page_end; i++) {
            number_span_html += '<a href="javascript:click_number_css(' + i + ')" class="number" title="' + i + '">' + i + '</a> ';
        }
        next_number_sign = true;
    }
}

/**
 * [ Load Pading 1.0]
 * @Author   :seven
 * @DateTime :2016-06-01T00:05:52+0800
 * @Detail   :用于缓存式分页,加载数据。
 */
function click_number_css(number) {
    LoadObj.jump_number_page(number);
}

function click_First_End(sign) {
    if (sign == 0) {
        LoadObj.jump_top();
    } else if (sign == 1) {
        LoadObj.jump_bottom();
    }
}

function click_search() {
    search_condition = $("#Loadsearch").val();
    //LoadObj.init_search_View();
    click_number_css(search_condition);
}
